<template>
	<div class="view">
		<!-- 头部 -->
		<div class="header">
			<div class="h_exit" style="cursor: pointer;" @click="to_exit">
				<i class="i-16 i-exit i-mr-10"></i>返回上一页
			</div>
			<div class="h_con">
				<div class="toggle_ul">
					<div class="toggle_li">
						广告管理</div>
				</div>
			</div>
		</div>
		<!-- 头部 end -->

		<!-- 主体部分 -->
		<div class="main">
			<div class="main_con" :style="{ height: conheight + 'px' }">
				<div class="item_box">
					<div class="box_top">
						<div class="box_title">全部广告({{ total }})</div>
						<div class="box_btn">
							<el-select v-model="sort" placeholder="请选择位置" style="width: 200px;margin-right: 10px"
								@change="search">
								<el-option v-for="item in cataList" :key="item.id" :label="item.sort" :value="item.sort">
								</el-option>
							</el-select>
							<el-input v-model="keywords" style="width: 200px;margin-right: 10px"
								prefix-icon="el-icon-search" placeholder="请输入关键词搜索" @change="search"></el-input>
							<el-button @click="search" type="primary" size="medium">搜索</el-button>
							<!-- <el-button icon="el-icon-setting" @click="sort_edit" type="primary" size="medium">位置设置</el-button> -->
							<el-button icon="el-icon-plus" @click="info_add" type="primary" size="medium">发布广告</el-button>
						</div>
					</div>
					<div class="table">
						<el-table ref="multipleTable" :data="infoList" tooltip-effect="dark" style="width: 100%"
							:height="table_height">
							<el-table-column type="index" label="行号" min-width="14%" align="center">
							</el-table-column>
							<!-- <el-table-column type="index" label="ID" min-width="14%" align="center">
							</el-table-column>
							<el-table-column prop="orderid" label="排序id" min-width="5%" align="center">
							</el-table-column> -->
							<el-table-column prop="title" label="广告标题" min-width="14%" align="center">
							</el-table-column>
							<el-table-column prop="sort" label="广告位置" min-width="14%" align="center">
							</el-table-column>
							<el-table-column prop="picdir" label="列表图(PC)" min-width="14%" align="center">
								<template slot-scope="scope">
									<img :src="$api.imgUrl+scope.row.picdir" alt="" v-if="scope.row.picdir" @click="imgOpen(scope.row)">
									<span v-else>-</span>
								</template>
							</el-table-column>
							<!-- <el-table-column prop="sort" label="列表图(手机端)" min-width="14%" align="center">
								<template slot-scope="scope">
									<img :src="$api.imgUrl+scope.row.picdirWap" alt="" v-if="scope.row.picdirWap">
									<span v-else>-</span>
								</template>
							</el-table-column> -->
							<el-table-column prop="timeCreate" label="创建时间" min-width="14%" align="center">
							</el-table-column>
							<el-table-column prop="isHidden" label="是否隐藏" min-width="14%" align="center">
								<template slot-scope="scope">
									<el-switch v-model="scope.row.isHidden" active-color="#13ce66" inactive-color="#999"
										:active-value="1" :inactive-value="0" @change="hiddenToggle(scope.row)">
									</el-switch>
								</template>
							</el-table-column>
							<el-table-column prop="sort" label="操作" min-width="14%" align="center">
								<template slot-scope="item">
									<el-button type="mini" @click="infoEdit(item.row)">编辑</el-button>
									<el-button type="mini" @click="infoRemove(item.row.id)">删除</el-button>
								</template>
							</el-table-column>
						</el-table>
						<div class="item_bottom">
							<div class="item_num">共{{total}}条信息</div>
							<div class="item_page">
								<el-pagination background layout="prev, pager, next, jumper" :total="total"
									@current-change="handleCurrentChange">
								</el-pagination>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!-- 主体部分 end -->

		<!-- 尾部 -->
		<template>
			<!-- <Footer></Footer> -->
		</template>
		<!-- 尾部 end -->
		<imgShow ref="imgShow"></imgShow>

	</div>
</template>

<script>
// import Footer from "../../components/footer.vue"
import {mapGetters} from "vuex"
import imgShow from "../../components/imgShow.vue"
export default {
	data() {
		return {
			left: 0,
			table_height: document.documentElement.clientHeight - 290,
			conheight: document.documentElement.clientHeight - 120,
			infoList: [],
			current: 1,
			limit: 10,
			total: 0,
			cataList: [
				{
					id: "0",
					sort: "全部",
				}
			],
			keywords: "",
			sort:"",
		}
	},
	created() {
		let that = this
	},
	components: {
		// Footer
		imgShow
	},
	created() {
		this.cataInit();
		this.data_init();
	},
	methods: {
		imgOpen(item){
			this.$refs.imgShow.open(item.picdir)
		},
		// 是否隐藏
		hiddenToggle(item){
			this.$http({
				url:this.$api.adUpdate,
				method:'post',
				data:{
					id:item.id,
					isHidden:item.isHidden
				}
			}).then(res =>{
				if(res.data.code==20000){
					this.$message.success(res.data.message)
					this.data_init();
				}else{
					this.$message.error(res.data.message)
					item.isHidden = item.isHidden==0? 1:0
				}
			})
		},
		search() {
			this.current = 1;
			this.data_init()
		},

		// 跳转广告管理目录列表
		sort_edit() {
			this.$router.push('/ad/sort')
		},

		// 跳转增加
		info_add() {
			this.$router.push('/ad/add')
		},

		// 加载列表数据
		data_init() {
			this.$http({
				'method': 'post',
				'url': this.$api.adList,
				data: {
					current: this.current,
					limit: this.limit,
					sort: this.sort=="全部"? '':this.sort,
					keywords: this.keywords
				}
			}).then(res => {
				if (res.data.code == 20000) {
					this.total = res.data.data.total
					this.infoList = res.data.data.list
				}
			})
		},

		// 列表数据分页
		handleCurrentChange: function (currentPage) {
			this.current = currentPage
			this.data_init()
		},

		// 列表数据删除
		infoRemove(id) {
			this.$confirm('是否删除这条目录?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(() => {
				this.$http({
					'method': 'get',
					'url': this.$api.adRemove + id
				}).then(res => {
					if (res.data.code == 20000) {
						this.$message({
							type: 'success',
							message: res.data.message
						});
						this.data_init()
					} else {
						this.$message.error(res.data.message)
					}
				})

			}).catch(() => {
				this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		// 列表数据修改
		infoEdit(item) {
			this.$store.commit("setUrlParma",{id:item.id})
			this.$router.push('/ad/update')
		},

		cataInit() {
			this.$http({
				url: this.$api.adSortList,
			}).then(res => {
				if (res.data.code == 20000) {
					this.cataList.push(...res.data.data.list);
				}
			})
		},
		to_exit() {
			this.$router.push('/index')
		},

	}

}
</script>

<style scoped>
@import url("../../assets/css/info.css");
@import url("../../assets/css/infoEdit.css");
</style>
